package com.hnkywl.jip.dao;

import com.hnkywl.jip.utils.Md5;
import com.hnkywl.jip.utils.UserData;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-3-11
 * Time: 上午10:52
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class UserDao extends SimpleHibernateTemplate {
    public Object[] login(String userName, String password) {
        String sql = "select manid,name,jiuqiname,jiuqipass from dbo.t_sys_user where name=:userName and pass=:password and status=1";
        SQLQuery query = createSQLQuery(sql);
        query.setString("userName", userName);
        query.setString("password", Md5.md5Encrypt16(password));
        Object[] result = (Object[]) query.uniqueResult();
        return result;
    }

    public Page<Object[]> searchUserMapping(String userName, Integer start, Integer limit) {
        String sql = StringUtils.EMPTY;
        SQLQuery query = null;
        Integer total = 0;
        List<Object[]> result;
        if (StringUtils.isNotEmpty(userName)) {
            sql = "select a.manid,a.name,a.jiuqiname,a.jiuqipass" +
                    " from dbo.t_sys_user a where a.name=:userName and a.status=1";
            String sqlCount = "select count(1)  from dbo.t_sys_user a where a.name=:userName and a.status=1";
            query = createSQLQuery(sqlCount);
            query.setString("userName", userName);
            total = (Integer) query.uniqueResult();
            query = createSQLQuery(sql);
            query.setString("userName", userName);
            result = query.setFirstResult(start).setMaxResults(limit).list();
        } else {
            sql = "select a.manid,a.name,a.jiuqiname,a.jiuqipass" +
                    " from dbo.t_sys_user a where  a.status=1";
            String sqlCount = "select count(1)  from dbo.t_sys_user a where  a.status=1";
            query = createSQLQuery(sqlCount);
            total = (Integer) query.uniqueResult();
            query = createSQLQuery(sql);
            result = query.setFirstResult(start).setMaxResults(limit).list();
        }
        Page<Object[]> page = new Page<Object[]>();
        page.setTotalCount(total);
        if (result != null && !result.isEmpty())
            page.setResult(result);
        return page;
    }

    public void saveUserMapping(UserData userData) {
        String sql = "update dbo.t_sys_user  set jiuqiname=:jiuqiname,jiuqipass=:jiuqipass where name=:name";
        SQLQuery query = createSQLQuery(sql);
        query.setString("name", userData.getUserName());
        query.setString("jiuqiname", userData.getJiuqiname());
        query.setString("jiuqipass", userData.getJiuqipass());
        query.executeUpdate();
    }
}
